package com.pachong.android.baseuicomponent.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.pachong.android.baseuicomponent.BaseComponentHelper;
import com.pachong.android.baseuicomponent.ILoadable;
import com.pachong.android.baseuicomponent.LoadState;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.view.BottomLoadStateView;

import java.util.List;

/**
 * 带RecyclerView的Activity.
 *
 * 对RecyclerView做了基本的设置,可以满足平时正常的需求,同时对外提供扩展,用户可以调用API修改设置和扩展功能
 *
 * @author Vivi Peng
 * @version 1.0.0
 * @e-mail pengveivei@sina.cn
 * @QQ 897058316
 * @since 1979-01-01
 */
public abstract class RecyclerViewBaseActivity extends BaseActivity implements ILoadable,
        AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private BaseComponentHelper mCompHelper;

    public RecyclerViewBaseActivity() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mCompHelper = new BaseComponentHelper(this, this);
        super.onCreate(savedInstanceState);
    }


    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = createRecyclerContentView(inflater);
        if (getRecyclerView() == null) {
            throw new RuntimeException("RecyclerView is null: 覆盖createRecyclerContentView()， 并且调用setRecyclerView");
        }

        mCompHelper.initRecyclerView(this, this);
        setAdapter(createAdapter());

        if (getAdapter() != null) {
            getRecyclerView().setLayoutManager(createLayoutManager());
            mCompHelper.setHeaderView(getAdapter(), createHeaderView(inflater));
            setBottomLoadStateView(createBottomLoadStateView(inflater));
        }
        return view;
    }


    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        mCompHelper.addItemDecoration(itemDecoration);
    }


    public void setAdapter(RecyclerViewBaseAdapter adapter) {
        mCompHelper.setAdapter(adapter, this, this, this);
    }

    /**
     * 获取包含RecyclerView的容器，用户可以覆盖此方法，并且设置RecyclerView
     *
     * @param inflater
     * @return
     */
    public View createRecyclerContentView(LayoutInflater inflater) {
        return mCompHelper.createRecyclerContentView(inflater);
    }


    public void setRecyclerView(RecyclerView rv) {
        mCompHelper.setRecyclerView(rv);
    }


    public abstract RecyclerView.LayoutManager createLayoutManager();


    public RecyclerView getRecyclerView() {
        return mCompHelper.getRecyclerView();
    }


    public RecyclerViewBaseAdapter getAdapter() {
        return mCompHelper.getAdapter();
    }


    /**
     * 回调方法，创建adapter
     *
     * @return
     */
    public abstract RecyclerViewBaseAdapter createAdapter();


    public View createHeaderView(LayoutInflater inflater) {
        return null;
    }


    /**
     * @param state
     */
    public void changeLoadState(LoadState state) {
        mCompHelper.setLoadState(state);
    }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {

    }


    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long itemId) {

        return false;
    }


    public void clearDecorations() {
        mCompHelper.clearDecorations();
    }


    public boolean isEmpty() {
        return mCompHelper.isEmpty();
    }


    public List getData() {
        return getAdapter().getData();
    }


    //region 加载功能接口

    /**
     * 设置触发自动加载的item的剩余数量
     *
     * @param itemCount
     */
    public void setTriggerLoadItemCount(int itemCount) {
        mCompHelper.setTriggerLoadItemCount(itemCount);
    }


    /**
     * 开始加载,由User调用
     */
    public void startLoading() {
        mCompHelper.startLoading(this, this, this);
    }

    /**
     * 是否自动加载
     *
     * @param auto
     */
    public void setAutoLoading(boolean auto) {
        mCompHelper.setAutoLoading(auto);
    }


    public BottomLoadStateView createBottomLoadStateView(LayoutInflater inflater) {
        return null;
    }


    public void setBottomLoadStateView(BottomLoadStateView stateView) {
        mCompHelper.setBottomLoadStateView(this, stateView);
    }


    /**
     * 回调方法，开始加载更多
     */
    public abstract void onStartLoading();
    //endregion


}
